package ccm.component.menu;

import ccm.component.override.JCheckBoxMenuItem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

abstract public class SingleMenuItem extends MenuItemJPanel implements SendableMenuItem
{
    protected JCheckBoxMenuItem autoSendJCheckBox;

    public SingleMenuItem(DelegateSenderInterface sender,Component[] rightMenu,String saveArgPrefix,int dataIndex,boolean canModify,ScnsType type,int page,String name)
    {
        super(sender,saveArgPrefix,dataIndex,canModify,type,page,name);
        if(rightMenu!=null)
        {
            JPopupMenu menu=new JPopupMenu();
            autoSendJCheckBox=new JCheckBoxMenuItem("自动发送");
            autoSendJCheckBox.addActionListener(e->
            {
                storeConfig();
                nameJLabel.setBackground(autoSendJCheckBox.isSelected()?Color.BLUE:null);
            });
            MouseAdapter mouseAdapter=new MouseAdapter()
            {
                public void mouseClicked(MouseEvent e)
                {
                    if(e.getButton()==MouseEvent.BUTTON3)
                    {
                        menu.removeAll();
                        for(Component ri: rightMenu)
                        {
                            menu.add(ri);
                        }
                        menu.add(clipJMenuItem);
                        if(canModify) menu.add(autoSendJCheckBox);
                        menu.show((Component)e.getSource(),e.getX(),e.getY());
                    }
                }
            };
            nameJLabel.addMouseListener(mouseAdapter);
            this.addMouseListener(mouseAdapter);
        }
    }

    protected boolean needSendValueFlag;

    @Override
    public boolean needSendValue()
    {
        return needSendValueFlag;
    }
}
